package cn.itsource.hrm.context;

import cn.itsource.hrm.domain.LoginUser;
import com.alibaba.fastjson.JSON;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;

public class LoginContext {

    public static LoginUser getLogin(){
        //获取登录用户
        try {
            SecurityContext securityContext = SecurityContextHolder.getContext();
            Authentication authentication = securityContext.getAuthentication();
            if(authentication != null){
                //AnonymousUser
                String principal = (String) authentication.getPrincipal();
                LoginUser loginUser = JSON.parseObject(principal , LoginUser.class);
                return loginUser;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
